﻿//-----------------------------------------------------------------------------------------------------------
// WBFSSync Project by Omega Frost 
// http://wbfssync.codeplex.com/
//
// WBFSSync is Licensed under the terms of the 
// Microsoft Reciprocal License (Ms-RL)
//-----------------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace WbfsSync.Core
{
    //-------------------------------------------------------------------------------------------------------
    //
    //-------------------------------------------------------------------------------------------------------
    public static class EnvVars
    {
        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static bool HasVar(String path)
        {
            return (path[0] == '%') && (path.IndexOf('%', 1) > 1);
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static bool VirtualPlace(String path)
        {
            if (path[0] != '%') return false;
            int varend = path.IndexOf('%', 1);

            String var = path.Substring(1, varend - 1).ToLower();
            if (var == "computer") return true;
            else if (var == "libraries") return true;
            else if (var == "favorites") return true;
            else if (var == "recent") return true;
            else return false;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static String Expand(String path)
        {
            if (path[0] != '%') return path;
            int varend = path.IndexOf('%', 1);

            String var = path.Substring(1, varend - 1).ToLower();
            if (String.CompareOrdinal(var, "desktop") == 0)
            {
                path = path.Remove(0, varend + 1);
                path = Environment.GetFolderPath(Environment.
                    SpecialFolder.DesktopDirectory) + path;
            }
            else if (String.CompareOrdinal(var, "computer") == 0)
            {
                if (path.Length <= 11) path = "%computer%";
                else path = path.Remove(0, 11);
            }
            else if (String.CompareOrdinal(var, "documents") == 0)
            {
                path = path.Remove(0, varend + 1);
                path = Environment.GetFolderPath(Environment.
                    SpecialFolder.MyDocuments) + path;
            }

            return path;
        }


        //---------------------------------------------------------------------------------------------------
        //
        //---------------------------------------------------------------------------------------------------
        public static string GetVarString(String var)
        {
            var = var.ToLower();

            if (var == "%computer%") return "Computer";
            else if (var == "%desktop%") return "Desktop";
            else if (var == "%documents%") return "Documents";
            else if (var == "%favorites%") return "Favorites";
            else if (var == "%libraries%") return "Libraries";
            else if (var == "%recent%") return "Recent Places";
            else if (var == "%downloads%") return "Downloads";
            else return var;
        }
    }
}
